// pages/detail/detail.js
import {getCommentList,getImagesByAtlasId,saveComment,getWork} from '../../service/config/index.js'

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    proData: [], //图片集合
    works:{},
    productId:0,
    baseUrl:app.globalData.url,
    index:1, // 顶部导航索引 默认选中第一列
    pageIndex: 1,
    pageTotal: 1, // 最大页数
    reachBottom: false,
    isFiexd:false,
    show:false,
    messageList:[
      
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id
    console.log(options)
    this.setData({
      productId:id
    })
    this._getProDetail(parseInt(id))
    this._getWorks(parseInt(id))
  },

  toMessage(){
    this.setData({show : true})
  },
  bindConfirm(val){
    console.log("留言内容",val.detail)
    let that = this
    let param = {}
    param.content = val.detail
    param.targetId = this.data.productId
    console.log(saveComment(param))
    saveComment(param).then(res=>{
      if(res.code ==0){
        wx.showToast({
          title: '成功',
        })
        that._getProDetail(that.data.productId)
      }
    })
  },

  /**
   * 
   * @param {点击按钮显示} e 
   */
  iconClick(e){
    let index = e.currentTarget.dataset.index
    this.setData({
      index:index
    })
  },

  /**
   * 预约服务
   */
  subServer(){
    wx.navigateTo({
      url: '/pages/detail/sub/sub'
    })
  },

  //预览图片
 previewImage: function (e) {
  var current = e.target.dataset.src;
  var imgList = [];
  for (let i = 0; i < this.data.listData.length; i++) {
   imgList.push(this.data.listData[i].url);
  }
  wx.previewImage({
   current: current,//当前点击的图片链接
   urls: imgList//图片数组
  })
 },

 _getProDetail(id){
   let that = this
   let param = {}
   param.atlasId = id
   param.tag = 2
   param.status = 1
   getImagesByAtlasId(param).then(res=>{
     if(res.code ==200){
      that.setData({proData:res.rows})
     }
   });

   let commParam = {}
   commParam.targetId = id
   getCommentList(commParam).then(res=>{
     if(res.code == 0 && res.data.length > 0){
       that.setData({
        messageList:res.data
       })
     }
   })
 },

 _getWorks(id){
   let that = this
   let param = {}
   param.id = id
  getWork(param).then(res=>{
    console.log(res)
    if(res.code == 200){
      that.setData({
        works:res.data
      })
    }
  })
 },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
})